;             # Body mass ratio between intermediate and apex species
Gamma = Alpha*Beta;             # Body mass ratio between basal and apex species

Mx = 0.001;           # Mass of basal resource A
My = Mx*Alpha;        # Mass of consumer B
Mz = My*Beta;         # Mass of Predator C

Tmin <- c(0, seq(1.1, 39.1, by = 40/Nsim));
Tmax <- seq(1, 40, by=40/Nsim);
Tr<-10^(-12);         # Minimum biomass threshold under which species are considered extinct

#################################### Section EQ: Species biomass at equilibrium ====
PPMR_Name <- paste(Alpha, Beta, sep='.');
Sec_Name <- paste(Sim, 'EQ', sep='.');
Fw_Name <- paste(Fw, PPMR_Name, Sec_Name, sep='_');

S1 <- EQ_2spAB(Tmin[Sim], Tmax[Sim], Alpha=Alpha);
data <- rbind(S1);
data$Bx[data$Bx == "NaN"] <- 0;    ##remove NaN
data$By[data$By == "NaN"] <- 0;
data$Bx[data$Bx < 0]      <- 0;    ##remove negative biomasses
data$By[data$By < 0]      <- 0;
data<-as.data.frame(data);

write.table(data, paste(Fw_Name, 'txt', sep='.'), dec= ".", sep = "\t", row.names=FALSE, col.names=TRUE);

#################################### Section ODE: System dynamic ====
Sec_Name <- paste(Sim, 'ODE', sep='.');
Fw_Name <- paste(Fw, PPMR_Name, Sec_Name, sep='_');

#### Creating the outcome data from the 80 200 simulations
outa <- mapply(TC, Time=Time, data$Temp[1:dim(data)[1]], data$Car[1:dim(data)[1]],
		Bx=data$Bx[1:dim(data)[1]]*1.02, By=data$By[1:dim(data)[1]]*1.02, Bz=1E-6,
		data$Mx[1:dim(data)[1]], data$My[1:dim(data)[1]], rep(Mz,dim(data)[1]),
		data$Alpha[1:dim(data)[1]], rep(Beta,dim(data)[1]), rep(Gamma,dim(data)[1]));
outb <- data.frame(matrix(unlist(outa), nrow=157, ncol=dim(data)[1], byrow=F), stringsAsFactors=FALSE);
outc <- t(outb);
bigdata <- cbind.data.frame(data$Temp[1:dim(data)[1]], data$Car[1:dim(data)[1]], data$Alpha[1:dim(data)[1]], rep(Beta,dim(data)[1]), rep(Gamma,dim(data)[1]), outc);

#### Data Part One: System's state ====
Trc <- as.data.frame(bigdata[,1:12]);
colnames(Trc)<-c("Temperature", "Car", "Alpha", "Beta", "Gamma", "Time", "A", "B", "C", "T_extA", "T_extB", "T_extC") ;

## Data correction
Trc$Afin <- (ifelse(Trc$A>Tr,1,0));
Trc$Bfin <- (ifelse(Trc$B>Tr,1,0));
Trc$Bfin[which(Trc$Afin == 0)]  <- 0;
Trc$Cfin <- (ifelse(Trc$C>Tr,1,0));
Trc$Cfin[which(Trc$Bfin == 0)]  <- 0;
Trc$Tot  <- (Trc$Afin+Trc$Bfin+Trc$Cfin);

Trc_cor <-cbind.data.frame(Trc$Time, Trc$Temperature, Trc$Car,
                        Trc$A, Trc$B, Trc$C, Trc$Afin, Trc$Bfin, Trc$Cfin, Trc$Tot,
                        Trc$T_extA, Trc$T_extB, Trc$T_extC,
                        data$Mx , data$My, rep(Gamma,dim(data)[1]),
                        Trc$Alpha, Trc$Beta, Trc$Gamma);

colnames(Trc_cor)<-c("Time", "Temperature", "Carr",
                      "A", "B", "C", "Afin", "Bfin", "Cfin", "Tot",
                      "ExtTimeA", "ExtTimeB", "ExtTimeC",
                      "M_A", "M_B", "M_C",
                      "Alpha", "Beta", "Gamma");

write.table(Trc_cor, paste(Fw_Name, 'txt', sep='.'), dec= ".", sep = "\t", row.names=FALSE, col.names=TRUE);

#################################### Section pop: metric ====
## Calculus of populations minima, mean, maxima, sd and cv
## for the last 10 years of the simulation
Sec_Name <- paste(Sim, 'POP', sep='.');
Fw_Name <- paste(Fw, PPMR_Name, Sec_Name, sep='_');

#### Data Part Two: System's metrics ====
sub_data <- data.frame();
for(i in 1:dim(bigdata)[1]){
  arrmat <- as.data.frame(matrix(unlist(bigdata[i,13:162]), ncol=15, byrow=T) );
  corrmat <- cbind( rep(bigdata[i,1],10), rep(bigdata[i,2], 10), seq(1, 10, by=1) , arrmat,
              rep(Mx, 10), rep(My, 10), rep(Mz, 10), rep(Alpha, 10), rep(Beta, 10), rep(Gamma, 10));
  sub_data <- rbind(sub_data, corrmat);
}
Trc_pop <- as.data.frame(sub_data)

title<-c("Temperature", "Carr", "Year");
for(j in 1:3){
  title<-append(title,paste(LETTERS[j],c('Min', 'Max', 'Mean', 'Sd', 'CV'), sep='_'));
}
colnames(Trc_pop) <- c(title, "M_A", "M_B", "M_C", "Alpha", "Beta", "Gamma");

write.table(Trc_pop, paste(Fw_Name, 'txt', sep='.'), dec= ".", sep = "\t", row.names=FALSE, col.names=TRUE)